install.packages("caret",repos = "http://cran.us.r-project.org")
## package 'caret' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\mjone\AppData\Local\Temp\Rtmp4csGIC\downloaded_packages
pacman::p_load(pacman,janitor,readr,gmodels,tidyr,dplyr,ggthemes,gapminder,plotly,cowplot,tmap,ggplot2,RColorBrewer,gganimate,gifski,sf,factoextra,e1071,cluster,kernlab,corrplot,corrgram,hrbrthemes,viridis,glmnet,rpart,rpart.plot,partykit,maps,tmap,ggmap,htmltools)
For our final project we will be reviewing Economic Freedom across the worls. With data ranging from 1970-2016. We start by reading in and cleaning the data. In total we have 3726 observations
freedom <- read_csv("C:\\Users\\mjone\\OneDrive\\Desktop\\MBA 8045\\Data\\efw_cc.csv") # Update to your file location
freedom <- freedom %>%
clean_names()
Now that we have access to all of our data we will take a look at it to see which variables we have access to
str(freedom) #3726 Observations
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 3726 obs. of 36 variables:
## $ year : num 2016 2016 2016 2016 2016 ...
## $ iso_code : chr "ALB" "DZA" "AGO" "ARG" ...
## $ countries : chr "Albania" "Algeria" "Angola" "Argentina" ...
## $ economic_freedom : num 7.54 4.99 5.17 4.84 7.57 7.98 7.58 6.49 7.34 7.56 ...
## $ rank : num 34 159 155 160 29 10 27 106 49 30 ...
## $ quartile : num 1 4 4 4 1 1 1 3 2 1 ...
## $ x1a_government_consumption : num 8.23 2.15 7.6 5.34 7.26 ...
## $ x1b_transfers : num 7.51 7.82 8.89 6.05 7.75 ...
## $ x1c_gov_enterprises : num 8 0 0 6 8 10 10 0 7 10 ...
## $ x1d_top_marg_tax_rate : num 8 4.5 9.5 4 5 5 3.5 6.5 10 10 ...
## $ x1_size_government : num 7.94 3.62 6.5 5.35 7 ...
## $ x2a_judicial_independence : num 2.67 4.19 1.84 3.69 3.87 ...
## $ x2b_impartial_courts : num 3.15 4.33 1.97 2.93 4.2 ...
## $ x2c_protection_property_rights : num 4.51 4.69 2.51 4.26 5.66 ...
## $ x2d_military_interference : num 8.33 4.17 3.33 7.5 5.83 ...
## $ x2e_integrity_legal_system : num 4.17 5 4.17 3.33 5 ...
## $ x2f_legal_enforcement_contracts : num 4.39 4.51 2.3 3.63 5.2 ...
## $ x2g_restrictions_sale_real_property: num 6.49 6.63 5.46 6.86 9.8 ...
## $ x2h_reliability_police : num 6.93 6.14 3.02 3.39 5.71 ...
## $ x2i_business_costs_crime : num 6.22 6.74 4.29 4.13 7.01 ...
## $ x2j_gender_adjustment : num 0.949 0.821 0.846 0.769 1 ...
## $ x2_property_rights : num 5.07 4.69 2.96 3.9 5.81 ...
## $ x3a_money_growth : num 8.99 6.96 9.39 5.23 9.08 ...
## $ x3b_std_inflation : num 9.48 8.34 4.99 5.22 9.26 ...
## $ x3c_inflation : num 9.74 8.72 3.05 2 9.75 ...
## $ x3d_freedom_own_foreign_currency : num 10 5 5 10 10 10 10 5 0 10 ...
## $ x3_sound_money : num 9.55 7.25 5.61 5.61 9.52 ...
## $ x4a_tariffs : num 8.96 6.87 6.99 6.42 8.55 ...
## $ x4b_regulatory_trade_barriers : num 7.49 2.48 2.02 4.81 7.19 ...
## $ x4c_black_market : num 10 5.56 10 0 10 ...
## $ x4d_control_movement_capital_ppl : num 6.41 1.59 2.04 4.7 6.83 ...
## $ x4_trade : num 8.21 4.13 5.26 3.98 8.14 ...
## $ x5a_credit_market_reg : num 7.1 5.1 7.06 5.42 9.1 ...
## $ x5b_labor_market_reg : num 6.92 5.03 4.56 5.15 6.23 ...
## $ x5c_business_reg : num 6.71 5.68 4.93 5.54 6.8 ...
## $ x5_regulation : num 6.91 5.27 5.52 5.37 7.38 ...
## - attr(*, "spec")=
## .. cols(
## .. year = col_double(),
## .. ISO_code = col_character(),
## .. countries = col_character(),
## .. `ECONOMIC FREEDOM` = col_double(),
## .. rank = col_double(),
## .. quartile = col_double(),
## .. `1a_government_consumption` = col_double(),
## .. `1b_transfers` = col_double(),
## .. `1c_gov_enterprises` = col_double(),
## .. `1d_top_marg_tax_rate` = col_double(),
## .. `1_size_government` = col_double(),
## .. `2a_judicial_independence` = col_double(),
## .. `2b_impartial_courts` = col_double(),
## .. `2c_protection_property_rights` = col_double(),
## .. `2d_military_interference` = col_double(),
## .. `2e_integrity_legal_system` = col_double(),
## .. `2f_legal_enforcement_contracts` = col_double(),
## .. `2g_restrictions_sale_real_property` = col_double(),
## .. `2h_reliability_police` = col_double(),
## .. `2i_business_costs_crime` = col_double(),
## .. `2j_gender_adjustment` = col_double(),
## .. `2_property_rights` = col_double(),
## .. `3a_money_growth` = col_double(),
## .. `3b_std_inflation` = col_double(),
## .. `3c_inflation` = col_double(),
## .. `3d_freedom_own_foreign_currency` = col_double(),
## .. `3_sound_money` = col_double(),
## .. `4a_tariffs` = col_double(),
## .. `4b_regulatory_trade_barriers` = col_double(),
## .. `4c_black_market` = col_double(),
## .. `4d_control_movement_capital_ppl` = col_double(),
## .. `4_trade` = col_double(),
## .. `5a_credit_market_reg` = col_double(),
## .. `5b_labor_market_reg` = col_double(),
## .. `5c_business_reg` = col_double(),
## .. `5_regulation` = col_double()
## .. )
Next we will create a data frame for 2016 our most recent year. From there we will do some plotting to get a better view of what has occured over time.
freedom2016 <- filter(freedom, year == "2016") #Filtering to 2016 most recent year in data
freedom2016 <- freedom2016[order(freedom2016$rank),]#Sorting data frame from lowest to highest
head(freedom2016)
## # A tibble: 6 x 36
## year iso_code countries economic_freedom rank quartile x1a_government_~
## <dbl> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 2016 HKG Hong Kong 8.97 1 1 7.92
## 2 2016 SGP Singapore 8.84 2 1 4.82
## 3 2016 NZL New Zeal~ 8.49 3 1 4.62
## 4 2016 CHE Switzerl~ 8.39 4 1 6.4
## 5 2016 IRL Ireland 8.07 5 1 3.74
## 6 2016 USA United S~ 8.03 6 1 6.71
## # ... with 29 more variables: x1b_transfers <dbl>, x1c_gov_enterprises <dbl>,
## # x1d_top_marg_tax_rate <dbl>, x1_size_government <dbl>,
## # x2a_judicial_independence <dbl>, x2b_impartial_courts <dbl>,
## # x2c_protection_property_rights <dbl>, x2d_military_interference <dbl>,
## # x2e_integrity_legal_system <dbl>, x2f_legal_enforcement_contracts <dbl>,
## # x2g_restrictions_sale_real_property <dbl>, x2h_reliability_police <dbl>,
## # x2i_business_costs_crime <dbl>, x2j_gender_adjustment <dbl>,
## # x2_property_rights <dbl>, x3a_money_growth <dbl>, x3b_std_inflation <dbl>,
## # x3c_inflation <dbl>, x3d_freedom_own_foreign_currency <dbl>,
## # x3_sound_money <dbl>, x4a_tariffs <dbl>,
## # x4b_regulatory_trade_barriers <dbl>, x4c_black_market <dbl>,
## # x4d_control_movement_capital_ppl <dbl>, x4_trade <dbl>,
## # x5a_credit_market_reg <dbl>, x5b_labor_market_reg <dbl>,
## # x5c_business_reg <dbl>, x5_regulation <dbl>
#options(repr.plot.width=8, repr.plot.height=10)
#Plot Below Covers All Countries and Year to get a view of changes
freedom %>% filter(!is.na(economic_freedom) | !is.na(year)) %>% group_by(year) %>%
ggplot(aes(year,economic_freedom, fill = economic_freedom, group = 1))+
geom_line(aes(color = economic_freedom))+
scale_color_viridis(option = "plasma",direction = -1, discrete=FALSE) +
facet_wrap(~countries)+theme_bw()+
theme(legend.position = "none", axis.text.x = element_blank(),
strip.text.x = element_text(size = 6))+
xlab(" ") + ylab("")+ ggtitle("ECONOMIC FREEDOM OF THE WORLD 1970-2016")
## Warning: Removed 4 rows containing missing values (geom_path).
#static 2016 Plot
a1 <- ggplotly(ggplot(freedom2016, aes(quartile,economic_freedom , size = -rank)) +
geom_jitter(aes(color=countries, alpha=0.5)) +
theme_bw()+ theme(legend.position= "none")+
xlab("Quartile") +
ggtitle("Economic Freedom Index 2016"), tooltip = c("countries"))
a1
#Yearly Scatter Plot for US, Potentially other countries
p <- ggplot(freedom, aes(x1a_government_consumption, economic_freedom, color = countries)) +
geom_point(aes(size = -rank, frame = year, ids = countries)) +
scale_x_log10()
## Warning: Ignoring unknown aesthetics: frame, ids
p <- ggplotly(p)
## Warning: Transformation introduced infinite values in continuous x-axis
p
jBrewColors <- brewer.pal(n = 8, name = "BuPu")
p1 <- plot_geo(freedom2016) %>%
add_trace(z = ~-rank, color = ~-rank,colors = jBrewColors,
text = ~freedom2016$countries, locations = ~freedom2016$iso_code) %>%
colorbar(title = 'Countries' , tickprefix = 'Rank') %>%
layout(title = 'Economic Freedom 2016 by Rank')
p1
p2 <- plot_geo(freedom2016) %>%
add_trace(z = ~x5c_business_reg, color = ~x5c_business_reg,colors = jBrewColors,
text = ~freedom2016$countries, locations = ~freedom2016$iso_code) %>%
colorbar(title = 'Countries' , tickprefix = 'Trade') %>%
layout(title = 'Economic Freedom 2016 by Trade')
p2
#Assigning Clusters Here
fviz_nbclust(na.omit(freedom2016[4:36]), kmeans, method = "silhouette")
gap_stat <- clusGap(na.omit(freedom2016[4:36]), FUN = kmeans, nstart = 25,
K.max = 10, B = 50)
fviz_gap_stat(gap_stat)
n_cluster <- 4
results <- kmeans(na.omit(freedom2016[4:36]),n_cluster)
results
## K-means clustering with 4 clusters of sizes 26, 34, 32, 29
##
## Cluster means:
## economic_freedom rank quartile x1a_government_consumption x1b_transfers
## 1 6.701923 92.53846 2.807692 5.643794 7.660434
## 2 7.827353 18.85294 1.000000 4.848356 6.391209
## 3 5.793438 135.87500 3.812500 6.300299 8.580960
## 4 7.292759 53.82759 1.931034 5.677600 7.186032
## x1c_gov_enterprises x1d_top_marg_tax_rate x1_size_government
## 1 6.000000 6.923077 6.556826
## 2 8.676471 5.941176 6.464303
## 3 4.468750 5.984375 6.333596
## 4 7.448276 6.137931 6.612460
## x2a_judicial_independence x2b_impartial_courts x2c_protection_property_rights
## 1 4.531053 3.992858 5.283979
## 2 6.897442 5.333483 7.279879
## 3 3.502248 3.442479 4.446322
## 4 5.166858 4.198570 5.889236
## x2d_military_interference x2e_integrity_legal_system
## 1 6.025641 5.544872
## 2 8.774510 7.990196
## 3 4.375000 4.791667
## 4 7.270115 6.063218
## x2f_legal_enforcement_contracts x2g_restrictions_sale_real_property
## 1 4.447582 7.508096
## 2 5.321731 8.311222
## 3 3.265367 6.470558
## 4 4.532511 7.809283
## x2h_reliability_police x2i_business_costs_crime x2j_gender_adjustment
## 1 5.067634 5.386412 0.8680815
## 2 7.567842 6.920993 0.9669095
## 3 4.507362 4.774881 0.7815347
## 4 5.703705 5.593773 0.9143553
## x2_property_rights x3a_money_growth x3b_std_inflation x3c_inflation
## 1 4.922193 8.625213 8.740109 8.919356
## 2 7.048105 8.780676 9.541676 9.776266
## 3 3.906946 8.556711 7.586815 7.543010
## 4 5.535438 8.668712 9.216567 9.465114
## x3d_freedom_own_foreign_currency x3_sound_money x4a_tariffs
## 1 6.346154 8.157708 7.391465
## 2 10.000000 9.524654 8.224081
## 3 3.906250 6.898196 6.813615
## 4 9.655172 9.251391 7.827085
## x4b_regulatory_trade_barriers x4c_black_market
## 1 5.682171 9.982093
## 2 7.880651 10.000000
## 3 4.061851 8.989404
## 4 6.552729 10.000000
## x4d_control_movement_capital_ppl x4_trade x5a_credit_market_reg
## 1 4.955593 7.002831 8.061171
## 2 6.771267 8.219000 9.178555
## 3 3.791417 5.914072 6.914557
## 4 6.230559 7.652593 8.815506
## x5b_labor_market_reg x5c_business_reg x5_regulation
## 1 6.225815 6.316754 6.867913
## 2 6.872708 7.586695 7.879319
## 3 5.572057 5.261378 5.915998
## 4 6.770012 6.650672 7.412063
##
## Clustering vector:
## [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 4 4 4
## [38] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1 1 1
## [75] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## [112] 3 3 3 3 3 3 3 3 3 3
##
## Within cluster sum of squares by cluster:
## [1] 5140.445 5007.579 9203.978 4697.174
## (between_SS / total_SS = 91.2 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
freedom2016 <- na.omit(freedom2016)
freedom2016$cluster_number <- results$cluster
#Cluster Plots Note to Megan correct the color to be specific to each cluster not scale
fviz_cluster(results, data = freedom2016[4:36])
jBrewColors2 <- brewer.pal(n = 8, name = "Spectral")
p3 <- plot_geo(freedom2016) %>%
add_trace(z = ~cluster_number, color = ~cluster_number,colors = jBrewColors2,
text = ~freedom2016$countries, locations = ~freedom2016$iso_code) %>%
colorbar(title = 'Countries' , tickprefix = 'Clusters') %>%
layout(title = 'Economic Freedom 2016 by Cluster')
p3
#Regression Adjust to include Clusters Now we get into the meat of our analysis. We start be prepping our data for regression. Gathering some test and training data
str(freedom)
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 3726 obs. of 36 variables:
## $ year : num 2016 2016 2016 2016 2016 ...
## $ iso_code : chr "ALB" "DZA" "AGO" "ARG" ...
## $ countries : chr "Albania" "Algeria" "Angola" "Argentina" ...
## $ economic_freedom : num 7.54 4.99 5.17 4.84 7.57 7.98 7.58 6.49 7.34 7.56 ...
## $ rank : num 34 159 155 160 29 10 27 106 49 30 ...
## $ quartile : num 1 4 4 4 1 1 1 3 2 1 ...
## $ x1a_government_consumption : num 8.23 2.15 7.6 5.34 7.26 ...
## $ x1b_transfers : num 7.51 7.82 8.89 6.05 7.75 ...
## $ x1c_gov_enterprises : num 8 0 0 6 8 10 10 0 7 10 ...
## $ x1d_top_marg_tax_rate : num 8 4.5 9.5 4 5 5 3.5 6.5 10 10 ...
## $ x1_size_government : num 7.94 3.62 6.5 5.35 7 ...
## $ x2a_judicial_independence : num 2.67 4.19 1.84 3.69 3.87 ...
## $ x2b_impartial_courts : num 3.15 4.33 1.97 2.93 4.2 ...
## $ x2c_protection_property_rights : num 4.51 4.69 2.51 4.26 5.66 ...
## $ x2d_military_interference : num 8.33 4.17 3.33 7.5 5.83 ...
## $ x2e_integrity_legal_system : num 4.17 5 4.17 3.33 5 ...
## $ x2f_legal_enforcement_contracts : num 4.39 4.51 2.3 3.63 5.2 ...
## $ x2g_restrictions_sale_real_property: num 6.49 6.63 5.46 6.86 9.8 ...
## $ x2h_reliability_police : num 6.93 6.14 3.02 3.39 5.71 ...
## $ x2i_business_costs_crime : num 6.22 6.74 4.29 4.13 7.01 ...
## $ x2j_gender_adjustment : num 0.949 0.821 0.846 0.769 1 ...
## $ x2_property_rights : num 5.07 4.69 2.96 3.9 5.81 ...
## $ x3a_money_growth : num 8.99 6.96 9.39 5.23 9.08 ...
## $ x3b_std_inflation : num 9.48 8.34 4.99 5.22 9.26 ...
## $ x3c_inflation : num 9.74 8.72 3.05 2 9.75 ...
## $ x3d_freedom_own_foreign_currency : num 10 5 5 10 10 10 10 5 0 10 ...
## $ x3_sound_money : num 9.55 7.25 5.61 5.61 9.52 ...
## $ x4a_tariffs : num 8.96 6.87 6.99 6.42 8.55 ...
## $ x4b_regulatory_trade_barriers : num 7.49 2.48 2.02 4.81 7.19 ...
## $ x4c_black_market : num 10 5.56 10 0 10 ...
## $ x4d_control_movement_capital_ppl : num 6.41 1.59 2.04 4.7 6.83 ...
## $ x4_trade : num 8.21 4.13 5.26 3.98 8.14 ...
## $ x5a_credit_market_reg : num 7.1 5.1 7.06 5.42 9.1 ...
## $ x5b_labor_market_reg : num 6.92 5.03 4.56 5.15 6.23 ...
## $ x5c_business_reg : num 6.71 5.68 4.93 5.54 6.8 ...
## $ x5_regulation : num 6.91 5.27 5.52 5.37 7.38 ...
## - attr(*, "spec")=
## .. cols(
## .. year = col_double(),
## .. ISO_code = col_character(),
## .. countries = col_character(),
## .. `ECONOMIC FREEDOM` = col_double(),
## .. rank = col_double(),
## .. quartile = col_double(),
## .. `1a_government_consumption` = col_double(),
## .. `1b_transfers` = col_double(),
## .. `1c_gov_enterprises` = col_double(),
## .. `1d_top_marg_tax_rate` = col_double(),
## .. `1_size_government` = col_double(),
## .. `2a_judicial_independence` = col_double(),
## .. `2b_impartial_courts` = col_double(),
## .. `2c_protection_property_rights` = col_double(),
## .. `2d_military_interference` = col_double(),
## .. `2e_integrity_legal_system` = col_double(),
## .. `2f_legal_enforcement_contracts` = col_double(),
## .. `2g_restrictions_sale_real_property` = col_double(),
## .. `2h_reliability_police` = col_double(),
## .. `2i_business_costs_crime` = col_double(),
## .. `2j_gender_adjustment` = col_double(),
## .. `2_property_rights` = col_double(),
## .. `3a_money_growth` = col_double(),
## .. `3b_std_inflation` = col_double(),
## .. `3c_inflation` = col_double(),
## .. `3d_freedom_own_foreign_currency` = col_double(),
## .. `3_sound_money` = col_double(),
## .. `4a_tariffs` = col_double(),
## .. `4b_regulatory_trade_barriers` = col_double(),
## .. `4c_black_market` = col_double(),
## .. `4d_control_movement_capital_ppl` = col_double(),
## .. `4_trade` = col_double(),
## .. `5a_credit_market_reg` = col_double(),
## .. `5b_labor_market_reg` = col_double(),
## .. `5c_business_reg` = col_double(),
## .. `5_regulation` = col_double()
## .. )
freedom$high_ef <- ifelse(freedom$economic_freedom > 6, 1, 0) # Converting streams to binary data
freedom <- na.omit(freedom)
index_training <- sample(dim(freedom)[1], 40, replace = FALSE) # replace = false will prevent code from selecting same observation twice
training_data <- freedom[index_training, ]
test_data <- freedom[-index_training, ]
Provide insights by cluster
Since we dont want to randomly select variables to use we will use the Lasso Method for variable selection
typeof(training_data[, 7:36])
## [1] "list"
X_training <- data.matrix(training_data[, 7:36])
X_test <- data.matrix(test_data[, 7:36])
model_linear_lasso <- cv.glmnet(X_training, training_data$economic_freedom, family="gaussian", nfolds = 10, type.measure = "mse", alpha = 1)
summary(model_linear_lasso)
## Length Class Mode
## lambda 42 -none- numeric
## cvm 42 -none- numeric
## cvsd 42 -none- numeric
## cvup 42 -none- numeric
## cvlo 42 -none- numeric
## nzero 42 -none- numeric
## call 7 -none- call
## name 1 -none- character
## glmnet.fit 12 elnet list
## lambda.min 1 -none- numeric
## lambda.1se 1 -none- numeric
model_linear_lasso$lambda
## [1] 0.65772163 0.59929145 0.54605205 0.49754229 0.45334200 0.41306835
## [7] 0.37637250 0.34293660 0.31247106 0.28471199 0.25941896 0.23637290
## [13] 0.21537418 0.19624093 0.17880743 0.16292267 0.14844907 0.13526126
## [19] 0.12324502 0.11229628 0.10232018 0.09323034 0.08494801 0.07740147
## [25] 0.07052533 0.06426006 0.05855137 0.05334983 0.04861038 0.04429197
## [31] 0.04035719 0.03677197 0.03350525 0.03052873 0.02781665 0.02534549
## [37] 0.02309387 0.02104227 0.01917293 0.01746966 0.01591771 0.01450362
coef(model_linear_lasso, s = 0.4)
## 31 x 1 sparse Matrix of class "dgCMatrix"
## 1
## (Intercept) 4.56304805
## x1a_government_consumption .
## x1b_transfers .
## x1c_gov_enterprises .
## x1d_top_marg_tax_rate .
## x1_size_government .
## x2a_judicial_independence .
## x2b_impartial_courts .
## x2c_protection_property_rights .
## x2d_military_interference .
## x2e_integrity_legal_system .
## x2f_legal_enforcement_contracts .
## x2g_restrictions_sale_real_property .
## x2h_reliability_police .
## x2i_business_costs_crime .
## x2j_gender_adjustment .
## x2_property_rights 0.06484596
## x3a_money_growth .
## x3b_std_inflation .
## x3c_inflation .
## x3d_freedom_own_foreign_currency .
## x3_sound_money .
## x4a_tariffs .
## x4b_regulatory_trade_barriers .
## x4c_black_market .
## x4d_control_movement_capital_ppl .
## x4_trade 0.18000302
## x5a_credit_market_reg .
## x5b_labor_market_reg .
## x5c_business_reg .
## x5_regulation 0.09948984
coef(model_linear_lasso, s = 500)
## 31 x 1 sparse Matrix of class "dgCMatrix"
## 1
## (Intercept) 6.9485
## x1a_government_consumption .
## x1b_transfers .
## x1c_gov_enterprises .
## x1d_top_marg_tax_rate .
## x1_size_government .
## x2a_judicial_independence .
## x2b_impartial_courts .
## x2c_protection_property_rights .
## x2d_military_interference .
## x2e_integrity_legal_system .
## x2f_legal_enforcement_contracts .
## x2g_restrictions_sale_real_property .
## x2h_reliability_police .
## x2i_business_costs_crime .
## x2j_gender_adjustment .
## x2_property_rights .
## x3a_money_growth .
## x3b_std_inflation .
## x3c_inflation .
## x3d_freedom_own_foreign_currency .
## x3_sound_money .
## x4a_tariffs .
## x4b_regulatory_trade_barriers .
## x4c_black_market .
## x4d_control_movement_capital_ppl .
## x4_trade .
## x5a_credit_market_reg .
## x5b_labor_market_reg .
## x5c_business_reg .
## x5_regulation .
as.double(coef(model_linear_lasso, s = 0.4))
## [1] 4.56304805 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
## [7] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
## [13] 0.00000000 0.00000000 0.00000000 0.00000000 0.06484596 0.00000000
## [19] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
## [25] 0.00000000 0.00000000 0.18000302 0.00000000 0.00000000 0.00000000
## [31] 0.09948984
as.double(coef(model_linear_lasso, s = 500))
## [1] 6.9485 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## [11] 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## [21] 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## [31] 0.0000
#After Lasso Linear Regression
mlr2 <- lm(economic_freedom ~ x3_sound_money + x4_trade + x5c_business_reg, data = training_data)
anova(mlr2)
## Analysis of Variance Table
##
## Response: economic_freedom
## Df Sum Sq Mean Sq F value Pr(>F)
## x3_sound_money 1 12.4509 12.4509 139.149 6.309e-14 ***
## x4_trade 1 5.8537 5.8537 65.420 1.298e-09 ***
## x5c_business_reg 1 4.6241 4.6241 51.678 1.859e-08 ***
## Residuals 36 3.2212 0.0895
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(mlr2)
Provide insights by cluster and prediction points
Finally we plot our linear regression with the variables selected from the variable selection
plot_grid(ggplot(freedom, aes(x3_sound_money, economic_freedom)) +
geom_point(color='#50C878', alpha = 0.5)+
xlab("Sound Money") + ylab("Economic Freedom ")+
geom_smooth(method = lm)+theme_bw(),
ggplot(freedom, aes(x4_trade, economic_freedom)) +
geom_point(color='#50C878', alpha = 0.5)+
xlab("Trade") + ylab("")+
geom_smooth(method = lm)+theme_bw(),
ggplot(freedom, aes(x5c_business_reg, economic_freedom)) +
geom_point(color='#50C878', alpha = 0.5)+
xlab("Business_Regulations") + ylab("")+
geom_smooth(method = lm)+theme_bw(),align = 'h')